var shell = new ActiveXObject('WScript.Shell');
var port = 36557
var lable = document.getElementById("label");
var msg = document.getElementById("msg");
window.onload = function () {
    HMod.winSize(300, 300);

    

    shell.run('cmd.exe /c node ./app/http-ws-server.js -e ../ext_js/main.js -ob f -p ' + port, 1, false);

    onConnect(port);

}
var ws;
function onConnect(port) {
    ws = new WebSocket("ws://127.0.0.1:" + port);
    ws.onopen = function () {
        /* 连接成功 */
        lable.innerText = "连接成功";
    };
    ws.onclose = function () {
        /* 连接断开 */
        lable.innerText = "连接断开";
    };
    ws.onerror = function (error) {
        /* 错误 */
        lable.innerText = "出现错误" + error.toString();
    };

    ws.onmessage = function (event) {
        msg.innerText = event.data;
    };
}

function onClickSend() {
    const val = document.getElementById("input").value;
    ws.send(val);
    console.log(val);
}


const origLog = console.log;
const xmlHttp = HMod.xmlHttp("http://127.0.0.1:" + port, "POST");
console.log = function () {
    const args = arguments;
    let str = "";
    for (let i = 0; i < args.length; i++) {
        str += args[i] + "\n";
        // lines.forEach((line, index) => console.log(line));
    }
    xmlHttp.jsonStr(str).send();
}
